/**
 * Created by Administrator on 2017/7/17.
 */
/**
 *
 */
export default {
    required: (type = 'string') => {
    switch (type) {
    case 'number':
        return {
            type: type,
            required: true,
            message: '请输入数字值',
            trigger: 'change'
        }
    case 'any':
        return {
                required: true,
                validator: (rule, value, callback) => {
                if (value || value == 0) {
            return callback();
        } else {
            return callback(new Error('不能为空'));
        }
    },
        trigger: 'change'
    }
    default:
        return {
            type: type,
            required: true,
            message: '不能为空',
            trigger: 'change'
        }
    }

},
// 只能输入数字
numberRequired: () => {
    return {
        type: 'number',
        required: true,
        message: '请输入数字值',
        trigger: 'change'
    }
},
// 验证字符长度
checkLength: (min, max) => {
    return {
            validator: (rule, value, callback) => {
            if (value) {
                if (value.length < min || value.length > max) {
                    return callback(new Error(`长度在${min}到${max}个字符`));
                } else {
                    callback();
                }
            } else {
                callback();
}
},
    trigger: 'change'
}
},
/**
 * 已废弃
 * @param min
 * @param max
 * @returns {{validator: (function(*, *=, *)), trigger: string}}
 */
checkNumber: (min, max) => {
    return {
        validator: (rule, value, callback) => {
        value = Number(value);
    if (isNaN(Number(value))) {
        callback(new Error('请输入数字值'));
    } else {
        if ((min || min === 0) && value < min) {
            callback(new Error(`必须大于等于${min}的数字`));
        }
        if (max && value > max) {
            callback(new Error(`必须小于等于${max}的数字`));
        } else {
            callback();
        }
    }
},
    trigger: 'change'
}
},
checkNumberRange: (min, max, leftClosed = true, rightClosed = true) => {
    return {
            validator: (rule, value, callback) => {
            if (isNaN(Number(value))) {
        return callback(new Error('请输入数字值'));
    } else {
        if (min !== undefined && min !== null) {
            if (leftClosed && value < min) return callback(new Error(`必须大于等于${min}的数`));
            if (!leftClosed && value <= min) return callback(new Error(`必须大于${min}的数`));
        }
        if (max !== undefined && max !== null) {
            if (rightClosed && value > max) return callback(new Error(`必须小于等于${max}的数`));
            if (!rightClosed && value >= max) return callback(new Error(`必须小于${max}的数`));
        }
        return callback();
    }
},
    trigger: 'change'
}
},
checkIntegerLength: (min, max) => {
    return {
            validator: (rule, value, callback) => {
            if (!value) {
        return callback();
    }
    if (!Number.isInteger(Number(value))) {
        return callback(new Error('请输入整数值'));
    } else {
        const valueStrLen = value.toString().length;
        if (valueStrLen > max) {
            return callback(new Error(`总位数至多${max}位`));
        }
        if (valueStrLen < min) {
            return callback(new Error(`总位数至少${min}位`));
        }
        return callback();
    }
},
    trigger: 'change'
}
},
// 验证范围
checkIntegerRange: (min = 1, max = 999999) => {
    return {
            validator: (rule, value, callback) => {
            if (!Number.isInteger(Number(value))) {
        callback(new Error('请输入整数值'));
    } else {
        if (min !== undefined) {
            if (value < min) callback(new Error(`必须大于${min}`));
        }
        if (max !== undefined) {
            if (value > max) callback(new Error(`必须小于${max}`));
        }
        callback();
    }
},
    trigger: 'change'
}
},
// 验证手机号
checkPhone: () => {
    return {
            required: true,
            validator: (rule, value, callback) => {

                if (!/^(0|86|17951)?(13[0-9]|15[012356789]|17[0678]|18[0-9]|14[57])[0-9]{8}$/.test(value)) {
                    callback(new Error('请输入正确的手机号'));
                } else {
                    callback();
                }
},
    trigger: 'change'
}
},
/**
 * 1. 验证座机和 手机号
 * **/
checkLine: () => {
    return {
            required: true,
            validator: (rule, value, callback) => {
                if (!/^0\d{2,3}-?\d{7,8}$/.test(value) && !/^(0|86|17951)?(13[0-9]|15[012356789]|17[0678]|18[0-9]|14[57])[0-9]{8}$/.test(value)) {
                    callback(new Error('请输入正确的手机号 或 座机号'));
                } else {
                    callback();
                }
},
    trigger: 'change'
}
},
/**
 * 最大数值为999999.99 最小为0.01
 * */
maxNumber: () => {
    return {
            validator: (rule, value, callback) => {
            if (value) {
                if (value > 999999.99) {
                    callback(new Error('数值最大为999999.99'));
                } else if (value < 0) {
                    callback(new Error('数值最小为0'));
                }
                else {
                    callback();
                }
            } else {
                callback();
}
},
    trigger: 'change'
}
},
checkLandline: () => {
},

checkDigets: (digets = 2) => {
    return {
        required: true,
        validator: (rule, value, callback) => {
        value += ''
    if (value.length === 0) {
        callback(new Error('不能为空'))
    }
    else {
        let zero = value.indexOf('.')
        if (zero !== -1) {
            value.trim().slice(zero + 1).length > digets ?
                callback(new Error('只能输入' + digets + '位小数')) :
                callback()
        } else {
            isNaN(value) ?
                callback(new Error('请输入正确的数值')) :
                callback()
        }
    }
},
    trigger: 'change'
}
},
/**
 *
 * @param M 最大位数 （0-65）
 * @param D 小数点后几位 （0-30）
 * @returns {{validator: (function(*, *=, *)), trigger: string}}
 */
checkDecimal(M = 8, D = 2){
    return {
            validator: (rule, value, callback) => {
            if (!value) {
        return callback();
    }
    if (isNaN(value)) {
        return callback(new Error('请输入合法的数字'));
    } else {
        if (Number(value) < 0) {
            return callback(new Error('必须输入大于等于0的值'))
        }
        const valueStr = Number(value).toString();
        const decPointIndex = valueStr.indexOf('.');
        const valueM = decPointIndex > 0 ? valueStr.length - 1 : valueStr.length;
        const valueD = decPointIndex > 0 ? valueStr.length - 1 - decPointIndex : 0;
        if (valueM > M) {
            return callback(new Error(`总位数不能大于${M}`));
        }
        if (valueD > D) {
            return callback(new Error(`小数位数不能大于${D}`));
        }
        return callback();
    }
},
    trigger: 'change'
}
},

/**
 * 英文、数字、下划线
 */
checkEnglishChar: () => {
    return {
            required: true,
            validator: (rule, value, callback) => {
            if (/^\w+$/.test(value)) {
        callback();
    } else {
        callback(new Error('只允许输入英文、数字、下划线'));
    }
},
    trigger: 'change'
}
},
/**
 * 中文、英文、下划线、数字且不能以下划线开头
 * @return {{validator: (function(*, *=, *)), trigger: string}}
 */
checkName: () => {
    return {
            required: true,
            validator: (rule, value, callback) => {
            if (/^(?!_)(?!.*?_$)[a-zA-Z0-9_\\u4e00-\u9fa5]+$/.test(value)) {
        callback()
    } else {
        callback(new Error('输入姓名'))
    }
},
    trigger: 'change'
}
},
checkPname: () => {
    return {
            required: true,
            validator: (rule, value, callback) => {
            if (/^(?!_)(?!.*?_$)[a-zA-Z0-9_\u4e00-\u9fa5]+$/.test(value)) {
        callback()
    } else {
        callback(new Error('输入产品名称'))
    }
},
    trigger: 'change'
}
},
/**
 * 英文、数字   liguochen
 */
checkAlphanumeric: () => {
    return {
            required: false,
            validator: (rule, value, callback) => {
            if (/^[0-9a-zA-Z]*$/.test(value)) {
        callback();
    } else {
        callback(new Error('只允许输入英文、数字'));
    }
},
    trigger: 'change'
}
},
/**
 * 验证排序输入 1-100的值
 * */
sortNumber: () => {
    return {
            required: true,
            validator: (rule, value, callback) => {
            let validNumber = /^([1-9]\d?|100)$/;
    if (!validNumber.exec(value)) {
        callback(new Error('请输入一个1-100 的值'));
    } else {
        callback();
    }
}, trigger: 'blur'
}
},
}
